package com.xiaoqi.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.xiaoqi.constant.MessageConstant;
import com.xiaoqi.dao.MemberDao;
import com.xiaoqi.dao.OrderDao;
import com.xiaoqi.dao.OrderSettingDao;
import com.xiaoqi.exception.SunnydayException;
import com.xiaoqi.pojo.Member;
import com.xiaoqi.pojo.Order;
import com.xiaoqi.pojo.OrderSetting;
import com.xiaoqi.service.OrderService;
import com.xiaoqi.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author PC
 * creationTime: 2022/11/21 11:36
 */
@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private MemberDao memberDao;

    /**
     * 提交预约
     * @param datemap
     * @return
     */
    @Override
    public Order submit(Map<String, String> datemap) throws Exception {
        //准备时间
        String orderDateinform = datemap.get("orderDate");

        //查询预约设置
        OrderSetting orderSetting = orderSettingDao.findByDate(orderDateinform);
        //先判断预约日期是否可以预约
            //不可以
            if (null==orderSetting){
                //抛出异常
                throw new SunnydayException(MessageConstant.SELECTED_DATE_CANNOT_ORDER);
            }
            //可以
            //判断是否约满 以预约人数大于等于可以预约则约满
            //约满
            if (orderSetting.getReservations()>=orderSetting.getNumber()){
                //抛出异常
                throw new SunnydayException(MessageConstant.ORDER_FULL);
            }
            //没约满
            //获取用户所填的手机号码
            String telephone = datemap.get("telephone");
            //通过用户传入的手机号码查询会员记录
            Member memberInDb = memberDao.findByTelephone(telephone);
            //判断是否是会员
            //不是会员
            if (null==memberInDb){
                //表单中的数据加入实体类
                Member member = new Member();
                member.setName(datemap.get("name"));
                member.setSex(datemap.get("sex"));
                member.setIdCard(datemap.get("idCard"));
                member.setPhoneNumber(datemap.get("telephone"));
                member.setBirthday(new Date());
                //注册会员
                memberDao.add(member);
            }
            //是会员
            // 判断是否重复预约
//            orderDao.findByCondition()
                    //重复
                        //抛出异常
                    //不重复
                        //则预约
                            //并且修改预约人数

        return null;
    }

    /**
     * 根据订单id查询预约信息
     * @param id
     * @return
     */
    @Override
    public Map findById4Detail(Integer id) {
        return orderDao.findById4Detail(id);
    }

    /**
     * 通过套餐id分组查询每个套餐被预约的个数以及名字
     * @return
     */
    @Override
    public List<Map> findGroupBySetmealID() {
        return orderDao.findGroupBySetmealID();
    }

    /**
     * 查询热门套餐
     */
    @Override
    public List<Map> findHotSetmeal() {
        return orderDao.findHotSetmeal();
    }
    /**
     * 根据日期统计订单数
     */
    @Override
    public Integer findOrderCountByDate(String date) {
        return orderDao.findOrderCountByDate(date);
    }
    /**
     * 根据日期统计指定日期之后的到诊数
     */
    @Override
    public Integer findVisitsCountAfterDate(String date) {
        return orderDao.findVisitsCountAfterDate(date);
    }

    /**
     * 根据日期统计到诊数
     */
    @Override
    public Integer findVisitsCountByDate(String date) {
        return orderDao.findVisitsCountByDate(date);
    }

    /**
     * 查询当周的统计订单数
     */
    @Override
    public Integer findOrderCountByWeek(String date) {
        return orderDao.findOrderCountByWeek(date);
    }

    /**
     * 根据日期统计当周到诊数
     */
    @Override
    public Integer findVisitsCountByWeek(String date) {
        return orderDao.findVisitsCountByWeek(date);
    }
    /**
     * 根据日期统计当月订单数
     */
    @Override
    public Integer findOrderCountByMonth(String date) {
        return orderDao.findOrderCountByMonth(date);
    }
    /**
     * 根据日期统计当月到诊数
     */
    @Override
    public Integer findVisitsCountByMonth(String date) {
        return orderDao.findVisitsCountByMonth(date);
    }
}
